﻿using FwProject.Comm.Sms;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
namespace Monitoring.Network.Services
{
  public  class TimeHelpService
    {
        public TimeHelpService() {
            int i = 0;
#if DEBUG
            i = 1000 * 60 * 10;
#else 
            i=  1000*60*10;
#endif

            Timer t = new Timer(i);
            t.Elapsed += new System.Timers.ElapsedEventHandler(TimerUp);
            t.AutoReset = true;
            t.Start();
            Console.Write($"点碗服务开启定时监听中\n");
        }

        private void TimerUp(object sender, System.Timers.ElapsedEventArgs e)
        {
            Console.Write($"======准备监听=======\n");
            Console.BackgroundColor = ConsoleColor.Red;
            using (HttpClient client = new HttpClient())
            {
                try
                {
                    using (HttpResponseMessage responseMessage = Task.Run(() => client.GetAsync("https://service.fanwanyigou.com/api/WebProbe/callBackMessage")).Result)
                    {
                        Console.BackgroundColor = ConsoleColor.Green;
                        Console.Write($"=====点碗服务监听中=====\n");
                        Byte[] resultBytes = responseMessage.Content.ReadAsByteArrayAsync().Result;
                        string msg = Encoding.UTF8.GetString(resultBytes);
                        if (!Convert.ToBoolean(msg))
                        {
                            send("点碗服务异常120");
                            Console.Write($"服务：https://service.fanwanyigou.com====》》》访问异常\n");
                          
                        }
                        Console.Write($"点碗服务正常\n");

                    }
                    using (HttpResponseMessage responseMessage = Task.Run(() => client.GetAsync("https://shop.fanwanyigou.com/api/WebProbe/callBackMessage")).Result)
                    {
                      
                        Console.Write($"=====商城服务监听中======\n");
                        Byte[] resultBytes = responseMessage.Content.ReadAsByteArrayAsync().Result;
                        string msg = Encoding.UTF8.GetString(resultBytes);
                        if (!Convert.ToBoolean(msg))
                        {
                            send("商城服务异常120");
                            Console.Write($"服务：https://shop.fanwanyigou.com====》》》访问异常\n");
                        }
                    }
                }
                catch (Exception exe)
                {
                    Console.Write($"{exe.ToString()}");
                    Console.Write($"服务异常\n");
                    send("导出都是错误");

                }
            }
        }
        private void send(string msg)
        {
            SmsObject sms = new SmsObject();
            sms.code = $"{msg}";
            sms.Mobile = "13206048151";
            SmsUntil smsUntil = new SmsUntil();
            Task.Run( async() =>  await smsUntil.Send(sms));
        }
    }
}
